$(function () {
    $('#save-product-btn').click(function (event) {
        event.preventDefault();
        var self = $(this);
        var dialog = $('#product-dialog');
        var name = $("input[name='name']").val();
        var price = $("input[name='price']").val();
        var compute = $("input[name='compute']").val();
        var income = $("input[name='income']").val();
        var days = $("input[name='days']").val();
        var num = $("input[name='num']").val();
        var priority = $("input[name='priority']").val();
        var product_img = $("input[name='product_img']").val();
        var small_img = $("input[name='small_img']").val();

        var submitType = self.attr("data-type");
        var product_id = self.attr('data-id');

        var url = '';
        if (submitType == 'update'){
            url = '/cms/update_product/';
        }else{
            url = '/cms/add_product/';
        }

        zlajax.post({
            'url':url,
            'data':{
                'name':name,
                'product_img':product_img,
                'price':price,
                'compute':compute,
                'income':income,
                'days':days,
                'num':num,
                'priority':priority,
                'small_img':small_img,
                'product_id':product_id,
            },
            'success':function (data) {
                dialog.modal('hide');
                if(data['code']==200){
                    window.location.reload()
                }else{
                    zlalert.alertInfo(data['message']);
                }
            },
            'fail':function () {
                zlalert.alertNetworkError();
            }
        })
    })
});


$(function () {
    $(".edit-product-btn").click(function (event) {
        var self = $(this);
        var dialog = $('#product-dialog');
        dialog.modal('show');

        var tr = self.parent().parent();
        var name = tr.attr("data-name");
        var price = tr.attr("data-price");
        var compute = tr.attr("data-compute");
        var income = tr.attr("data-income");
        var days = tr.attr("data-days");
        var num = tr.attr("data-num");
        var product_img = tr.attr("data-img");
        var priority = tr.attr("data-priority");
        var small_img = tr.attr("data-small");

        var nameInput = dialog.find("input[name='name']");
        var imageInput = dialog.find("input[name='product_img']");
        var priceInput = dialog.find("input[name='price']");
        var computeInput = dialog.find("input[name='compute']");
        var incomeInput = dialog.find("input[name='income']");
        var daysInput = dialog.find("input[name='days']");
        var numInput = dialog.find("input[name='num']");
        var priorityInput = dialog.find("input[name='priority']");
        var smallInput = dialog.find("input[name='small_img']");
        var savebtn = dialog.find("#save-product-btn");

        nameInput.val(name);
        imageInput.val(product_img);
        smallInput.val(small_img);
        priceInput.val(price);
        computeInput.val(compute);
        incomeInput.val(income);
        daysInput.val(days);
        numInput.val(num);
        priorityInput.val(priority);
        savebtn.attr("data-type",'update');
        savebtn.attr("data-id",tr.attr('data-id'));
    })
});


$(function () {
    $('.delete-product-btn').click(function (event) {
        var self = $(this)
        var tr = self.parent().parent();
        var product_id = tr.attr('data-id');
        zlalert.alertConfirm({
            'msg':'您确定要删除这个产品吗？',
            'confirmCallback':function () {
                zlajax.post({
                    'url':'/cms/del_product/',
                    'data':{
                        'product_id':product_id
                    },
                    'success':function (data) {
                        if(data['code']==200){
                            window.location.reload()
                        }else{
                            zlalert.alertError(data['message'])
                        }
                    }
                })
            }
        })
    })
});



